package com.tool;

import com.learn.domain.Person;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * rest接口返回对象
 */
@Setter
@Getter
public class ResultResponse<T> implements Serializable {

    private Boolean success;
    private String message;
    private Integer code;
    private T data;

    public ResultResponse<T> success(Boolean state) {
        setSuccess(state);
        return this;
    }

    public ResultResponse<T> message(String msg) {
        setMessage(msg);
        return this;
    }

    public ResultResponse<T> code(Integer code) {
        setCode(code);
        return this;
    }

    public ResultResponse<T> data(T data) {
        setData(data);
        return this;
    }

    public static <T>ResultResponse<T> succeed() {
        ResultResponse<T> response = new ResultResponse<>();
        return response.success(true).code(200);
    }

    public static <T>ResultResponse<T> failure() {
        ResultResponse<T> response = new ResultResponse<>();
        return response.success(false).code(500);
    }

    public static void main(String[] args) {
        ResultResponse.<Person>succeed().data(null).message("请求成功");
        ResultResponse.<Person>failure().message("请求失败");
    }

}
